package com.personal.book.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.personal.book.dao.impl.AdminDaoImpl;
import com.personal.book.entity.Admin;
import com.personal.book.entity.Users;
import com.personal.book.service.impl.AdminServiceImpl;

/**
 * Servlet implementation class AdminServlet
 */
@WebServlet("/AdminServlet")
public class AdminServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	AdminDaoImpl adi = new AdminDaoImpl();
	AdminServiceImpl asi = new AdminServiceImpl();
	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AdminServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 编码
		request.setCharacterEncoding("utf-8");
		String op = "";
		if (request.getParameter("op") != null) {
			op = request.getParameter("op");
		}
		// 登录
		if ("login".equals(op)) {
			doLogin(request, response);

		}
		//登录后在管理员信息中显示信息
		else if("admininfo".equals(op)){
			doUserInfo(request,response);
		}//登录后在管理员信息中显示信息
		else if("update".equals(op)){
			doUpdate(request,response);
		}
	}

	
	/**
	 * 登录.
	 */
	private void doLogin(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 1.设置响应头的contentType
		response.setContentType("application/json;charset=utf-8");
		String adminName = request.getParameter("adminname");
		String adminPwd = request.getParameter("adminpwd");
		// 2 调用 service的方法,查询是否存在typename对象
		boolean flag = adi.Login(adminName, adminPwd);
		System.out.println(flag);
		if (flag) {
			request.getSession().setAttribute("adminName", adminName);
			
			request.getRequestDispatcher("adminindex.jsp").forward(request, response);
		} else {
			request.setAttribute("msg", "登录失败!请重新登录!");
			request.getRequestDispatcher("adminLogin.jsp").forward(request, response);
		}
	}
	
	/**
	 * 管理员个人信息页面显示信息
	 */
	private void doUserInfo(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 1.设置响应头的contentType
		response.setContentType("application/json;charset=utf-8");
		String adminName = (String) request.getSession().getAttribute("adminName");
		Admin admin = asi.getAdminByName(adminName);
		// 2 将管理员信息传值给admininfo.jsp中
		request.getSession().setAttribute("admin", admin);
	
		System.out.println(admin);
		request.getRequestDispatcher("adminInfo.jsp").forward(request, response);
	}
	/**
	 * 获取信息，修改用户
	 */
	private void doUpdate(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 1.设置响应头的contentType
		response.setContentType("application/json;charset=utf-8");
		Integer adminId = Integer.parseInt(request.getParameter("adminId"));
		String adminName = request.getParameter("adminName");
		String adminPwd = request.getParameter("adminPwd");
		String tel = request.getParameter("tel");
		String addr = request.getParameter("addr");
		
		Admin admin=new Admin(adminId, adminName, adminPwd, tel, addr);
		
		// 2 调用 service的方法,修改用户信息

		boolean flag = asi.Update(admin);
		System.out.println(flag);
		if (flag) {
			request.getRequestDispatcher("adminindex.jsp").forward(request, response);
		} else {
			request.setAttribute("msg", "修改失败!");
			request.getRequestDispatcher("adminInfo.jsp").forward(request, response);

		}
	}

	

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
